/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CapEvents;

import AreaDeDesenho.Frame;
import AreaDeDesenho.PainelDeDesenho;
import Formas.Forma;
import Formas.Linha;
import Formas.Retangulo;
import Helpers.EFormas;
import Helpers.ETransformacoes;
import Helpers.Funcoes;
import Helpers.Transformer;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author aluno
 */
public class MouseHandler implements MouseListener, ActionListener, MouseMotionListener {

    public PainelDeDesenho tela;
    public Forma selectedForma;
    public Point[] cliqueLinha;

    public MouseHandler(PainelDeDesenho tela)
    {
        this.tela = tela;
        cliqueLinha = new Point[2];
        cliqueLinha[0] = null;
        cliqueLinha[1] = null;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
       
 
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
       Component c = this.tela;
           while(c != null)
           {
               if(c.getParent() instanceof Frame)
               {
                 Frame frame = (Frame)c.getParent();
                 frame.labelXval.setText("" + e.getX());
                 frame.labelYval.setText("" + e.getY());
                 break;
               }else
                   c = c.getParent();
           }

        if(PainelDeDesenho.getFormaAtual() != EFormas.NONE)
        {
          if(this.tela.filaTransf != null)
          for(Transformer et: this.tela.filaTransf)
             this.tela.Transformar((Graphics2D) this.tela.getGraphics(), et);

          if (PainelDeDesenho.getFormaAtual() == EFormas.TRIANGULO)
            tela.addForma(Funcoes.desenhaForma(PainelDeDesenho.getFormaAtual(),PainelDeDesenho.getCorDoPincel(),this.tela.getGraphics(), e.getX(), e.getY(), 100, 100,5,5,5,-1,-1));
          else
          {
              if(cliqueLinha[0]==null)
                  cliqueLinha[0] =  new Point(e.getX(), e.getY());
          }
        }

        selectedForma = null;
        if(PainelDeDesenho.getFormaAtual() == EFormas.NONE && this.tela.filaFormas != null)
        {
          for(Forma f : this.tela.filaFormas)
          {
             if(f.pertenceAForma(e.getX(), e.getY()))
             {
                  selectedForma = f;
                          break;
              }
          }
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
     cliqueLinha[0] = null;
     cliqueLinha[1] = null;
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {
 
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JMenuItem item = (JMenuItem)e.getSource();
            if(item.getText().equals("Salvar"))
            {
            try {
                JFileChooser chooser = new JFileChooser();
                chooser.setDialogType(JFileChooser.SAVE_DIALOG);
                chooser.setFileFilter(new FileNameExtensionFilter("Arquivos PNG", "png"));
                if(chooser.showSaveDialog(tela) == JFileChooser.APPROVE_OPTION)
                {
                  String path = chooser.getSelectedFile().getAbsolutePath();
                  this.tela.SalvarImagem(path.substring(path.length() - 3).equalsIgnoreCase("png") ? path : path + ".png");
                }
            } catch (IOException ex) {
                Logger.getLogger(MouseHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
            tela.repaint();
        }
        else if(item.getText().equals("Limpar Tela"))
        {
           if(JOptionPane.showConfirmDialog(this.tela, "Tem certeja que deseja limapar a tela? \n Você perderá o desenho caso não tenha salvo!","Atenção!",JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE) == JOptionPane.YES_OPTION)
           {
           this.tela.filaFormas.clear();
           this.tela.filaTransf.clear();
           this.tela.refazer.clear();
           this.tela.imagem = null;
           this.tela.repaint();
           this.tela.paint(this.tela.getGraphics());
            }
        }
        else if(item.getText().equals("Abrir"))
        {
           try {
                JFileChooser chooser = new JFileChooser();
                chooser.setDialogType(JFileChooser.OPEN_DIALOG);
                chooser.setFileFilter(new FileNameExtensionFilter("Arquivos PNG", "png"));
                if(chooser.showOpenDialog(tela) == JFileChooser.APPROVE_OPTION)
                {
                  String path = chooser.getSelectedFile().getAbsolutePath();
                  this.tela.AbrirImagem(path);
                }
            } catch (Exception ex) {
                Logger.getLogger(MouseHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    @Override
    public void mouseDragged(MouseEvent e)
    {
      if(PainelDeDesenho.getFormaAtual() == EFormas.NONE && selectedForma != null)
      {
          if(this.tela.filaTransf != null)
          for(Transformer et: this.tela.filaTransf)
             this.tela.Transformar((Graphics2D) this.tela.getGraphics(), et);

          selectedForma.setsX(e.getX());
          selectedForma.setsY(e.getY());
          selectedForma.drawForma(this.tela.getGraphics());
          this.tela.repaint();
      }
      else if(PainelDeDesenho.getFormaAtual() == EFormas.LINE)
      {
          if(this.tela.filaTransf != null)
          for(Transformer et: this.tela.filaTransf)
             this.tela.Transformar((Graphics2D) this.tela.getGraphics(), et);

            cliqueLinha[1] = new Point(e.getX(), e.getY());
            if(selectedForma == null)
                tela.addForma(selectedForma = Funcoes.desenhaForma(PainelDeDesenho.getFormaAtual(),PainelDeDesenho.getCorDoPincel(),this.tela.getGraphics(), cliqueLinha[0].x, cliqueLinha[0].y, 100, 100,5,5,5, cliqueLinha[1].x,cliqueLinha[1].y));
            else
            {
               Linha l = (Linha)selectedForma;
               l.setsX1(e.getX());
               l.setsY1(e.getY());
               l.drawForma(this.tela.getGraphics());
               this.tela.repaint();
            }
      }else
      {
          if(this.tela.filaTransf != null)
          for(Transformer et: this.tela.filaTransf)
             this.tela.Transformar((Graphics2D) this.tela.getGraphics(), et);
          
            cliqueLinha[1] = new Point(e.getX(), e.getY());
            if(selectedForma == null)
                 tela.addForma(selectedForma = Funcoes.desenhaForma(PainelDeDesenho.getFormaAtual(),PainelDeDesenho.getCorDoPincel(),this.tela.getGraphics(), cliqueLinha[0].x, cliqueLinha[0].y, 100, 100,5,5,5, cliqueLinha[1].x,cliqueLinha[1].y));
            else
            {
               Retangulo l = (Retangulo)selectedForma;
               l.setHeight(e.getY() - cliqueLinha[0].y);
               l.setWidth(e.getX() - cliqueLinha[0].x);
               l.drawForma(this.tela.getGraphics());
               this.tela.repaint();
            }
      }
    }

    @Override
    public void mouseMoved(MouseEvent e) {

    }

}
